package com.neu.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.api.constan.StatusConst;
import com.neu.api.dto.FoodDTO;
import com.neu.api.mapper.FoodMapper;
import com.neu.api.pojo.Food;
import com.neu.api.query.FoodQuery;
import com.neu.api.service.FoodService;
import com.neu.api.utils.PageResult;
import com.neu.api.vo.FoodVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class FoodServiceImpl implements FoodService {

    @Resource
    private FoodMapper foodMapper;

    /**
     * 查询菜品
     * @return
     */
    @Override
    public PageResult<FoodVO> find(FoodQuery foodQuery) {

        //设置是否需要分页
        PageHelper.startPage(foodQuery.getPage(),foodQuery.getPageSize());  //告诉PageHelper此处需要分页

        com.github.pagehelper.Page<FoodVO> page = (com.github.pagehelper.Page<FoodVO>) foodMapper.find(foodQuery);

        PageInfo<FoodVO> pageInfo = page.toPageInfo();
        //查询语句
        //List<FoodVO> foodVOList = foodMapper.find(foodQuery);

        List<FoodVO> foodVOList = pageInfo.getList();

        long total = pageInfo.getTotal();

        PageResult<FoodVO> pageResult = new PageResult<>();
        pageResult.setTotal(total);
        pageResult.setList(foodVOList);
        return pageResult;
        //return foodMapper.find(foodQuery);我写的
    }

    /**
     * 添加菜品
     * @param foodDTO
     */
    @Override
    public void add(FoodDTO foodDTO) {

        //步骤
            //第一:先把dto--->pojo
            //第二:调用mapper/dao执行insert 语句


        //方式一:将dto-->pojo
        /*
        Food food = new Food();
        food.setName(foodDTO.getName());
        food.setPassword(foodDTO.getPassword());
        */

        //方式二:将dto-->pojo
//        Food food = new Food();
//        BeanUtils.copyProperties(foodDTO,food);

        Food food = new Food();  //name loginName phone
        BeanUtils.copyProperties(foodDTO,food);
        food.setCreateTime(new Date());
        food.setUpdateTime(new Date());

        food.setStatus(StatusConst.ACTIVE_STATUS);

        foodMapper.add(food);



    }
    

    @Override
    public void update(Long id, FoodDTO foodDTO) {
        Food food = new Food();

        BeanUtils.copyProperties(foodDTO,food);

        food.setUpdateTime(new Date());
        food.setId(id);
        foodMapper.update(food);
    }

    @Override
    public void deleteById(long id) {
        foodMapper.deleteById(id);
    }

    /**
     * 启用禁用账号
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(long id, byte status) {
        Food food = new Food();
        food.setId(id);
        food.setUpdateTime(new Date());
        food.setStatus(status);
        foodMapper.update(food);
    }

    @Override
    public int findCountByCategoryId(long categoryId) {
        return foodMapper.findCountByCategoryId(categoryId);
    }

    @Override
    public FoodVO findById(long id) {
        return foodMapper.findById(id);
    }

}
